package com.dyh.quickdemozwei.xx;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.List;
import java.util.function.Consumer;

/*用不了*/
@Slf4j
@Service
public class TcRunnableService {
//    @Autowired
//    private PlatformTransactionManager txManager;

    @Autowired
    PlatformTransactionManager transactionManager;

    @Autowired
    TransactionDefinition transactionDefinition;

    public void withNonTc(String topic, Consumer<Exception> exceptionConsumer, Runnable... runnables){


        try{
            for (Runnable runnable : runnables) {
                runnable.run();
            }
        }
        catch (Exception ex){
            log.error("topic={} ex=",topic,ex);
            if (exceptionConsumer!=null)
                exceptionConsumer.accept(ex);
        }
    }

    public void withNonTc(String topic, Consumer<Exception> exceptionConsumer, List<Runnable> runnables){


        try{
            for (Runnable runnable : runnables) {
                runnable.run();
            }
        }
        catch (Exception ex){
            log.error("topic={} ex=",topic,ex);
            if (exceptionConsumer!=null)
                exceptionConsumer.accept(ex);
        }
    }



//    public static void main(String[] args) {
//        TcRunableService tcRunableService = new TcRunableService();
//        int i =1;
//        tcRunableService.withNonTc("dawdawd",ex->log.error("{}",i),);
//    }

    public void withTc(String topic, Consumer<Exception> exceptionConsumer, Runnable... runnables){
        //开启事务
//         DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
//         definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);//创建一个新事务
//        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);

        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        //3.设置事务隔离级别，开启新事务
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        //4.获得事务状态
        TransactionStatus transactionStatus = transactionManager.getTransaction(def);
        Exception ex = null;
        try {
            //do something 业务代码
            for (Runnable runnable : runnables) {
                runnable.run();
            }

        } catch (Exception e) {
            ex = e;
            //手动回滚
            transactionManager.rollback(transactionStatus);

            log.error("topic={} ex=",topic,ex);
            if (exceptionConsumer!=null)
                exceptionConsumer.accept(ex);
        }
        if (ex==null){
            //提交
            transactionManager.commit(transactionStatus);
        }
    }

    public void withTc(String topic, Consumer<Exception> exceptionConsumer, List<Runnable> runnables){
        //开启事务
//         DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
//         definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);//创建一个新事务
//        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);

        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        //3.设置事务隔离级别，开启新事务
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        //4.获得事务状态
        TransactionStatus transactionStatus = transactionManager.getTransaction(def);
        Exception ex = null;
        try {
            //do something 业务代码
            for (Runnable runnable : runnables) {
                runnable.run();
            }

        } catch (Exception e) {
            ex = e;
            //手动回滚
            transactionManager.rollback(transactionStatus);

            log.error("topic={} ex=",topic,ex);
            if (exceptionConsumer!=null)
                exceptionConsumer.accept(ex);
        }
        if (ex==null){
            //提交
            transactionManager.commit(transactionStatus);
        }
    }

}
